package com.si.anddos.search;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.client.utils.URLEncodedUtils;

import com.si.anddos.Globals;

import android.util.Log;
import android.widget.Toast;

public class SearchMaker {
	private static final String KEY = "5eafe08c20933d0ddffd3d509fb766db";
	private static final String URL = "http://api.filestube.com/?key=" + KEY + "&phrase=";
	
	protected HttpRetriever httpRetriever = new HttpRetriever();
	protected XmlParser xmlParser = new XmlParser();
	
	public ArrayList<SearchResult> makeSearch(String phrase){		
		
		String url = constructSearchUrl(phrase);
		String response=null;
		ArrayList<SearchResult> result = new ArrayList<SearchResult>();
		//Log.d(getClass().getSimpleName(), response);
		
		
		
		for(int i=1;i<=Globals.MAX_SEARCHS;i++){
			response = httpRetriever.retrieve(url+"&page="+Integer.toString(i)+"&extension="+Globals.extension);
			
			if(response == null){
				response = "";
			}
			else{
				result.addAll(xmlParser.parseSearchResultResponse(response));
				response.trim();
			}
		}
		
		ArrayList<Integer> toDelete = new ArrayList<Integer>();
		 //if(result == null) result = new ArrayList<SearchResult>();
		for (int i = 0; i < result.size(); i++) {
			//Log.v("AAAAAAAAAAAAAAa", String.valueOf(i));
			SearchResult item = result.get(i);
			String itemResponse = httpRetriever.retrieve(item.getAddress());
			//Log.v("AAAAAAAAAAAAAAa", itemResponse);
			if(itemResponse == null) itemResponse = "";
			int iframeTagPos = itemResponse.indexOf("<iframe");
			if(iframeTagPos == -1)
			{
				toDelete.add(i);
				continue;
			}
			itemResponse = itemResponse.substring(iframeTagPos);
	        int start = itemResponse.indexOf("src");
	        int end = itemResponse.indexOf("</iframe>");
	        itemResponse = itemResponse.substring(start,end);
	        start = itemResponse.indexOf("\"");
	        end = itemResponse.indexOf(" ");
	        itemResponse = itemResponse.substring(start+1,end-1);
	        //Log.v("AAAAAAAAAAAAAAa", itemResponse);
	        
	        //If the item is inside the providers we want to show, isn't removed from the list
	        if(isToDelete(itemResponse))
	        	toDelete.add(i);
	        else{
	        	if(Globals.megaupload && itemResponse.contains("megaupload.com"))
	        		item.setAddress(itemResponse);
	        	if(Globals.rapidshare && itemResponse.contains("rapidshare.com"))
	        		item.setAddress(itemResponse);
	        	if(Globals.fileserve && itemResponse.contains("fileserve.com"))
	        		item.setAddress(itemResponse);
	        	if(Globals.megashares && itemResponse.contains("megashares.com"))
	        		item.setAddress(itemResponse);
	        	if(Globals.filesonic && itemResponse.contains("filesonic.com"))
	        		item.setAddress(itemResponse);
	        	if(Globals.hotfile && itemResponse.contains("hotfile.com"))
	        		item.setAddress(itemResponse);
	        }
	        
		}
		
		for(int i = 0; i < toDelete.size(); i++){
			result.remove(toDelete.get(i).intValue()-i);
		}
		
		return result;
	}


	private boolean isToDelete(String a){
		if(Globals.megaupload && a.contains("megaupload.com"))
			return false;
		if(Globals.rapidshare && a.contains("rapidshare.com"))
			return false;
		if(Globals.fileserve && a.contains("fileserve.com"))
			return false;
		if(Globals.megashares && a.contains("megashares.com"))
			return false;
		if(Globals.filesonic && a.contains("filesonic.com"))
			return false;
		if(Globals.hotfile && a.contains("hotfile.com"))
			return false;
		return true;
	}
	
	private String constructSearchUrl(String phrase) {
		return URL + URLEncoder.encode(phrase);
	}
}
